Crate safecast

source ·
Expand description

safecast defines traits analogous to From, Into, TryFrom, and TryInto to standardize the implementation of casting between Rust types. The can_cast_from and can_cast_into methods borrow the source value, allowing pattern matching without moving.

Macros

Traits

Conversion methods from a container type (such as an enum) and a target type T.
Trait for defining a cast operation from some source type T. Analogous to From. The inverse of CastInto. Prefer implementing CastFrom over CastInto because implementing CastFrom automatically provides an implementation of CastInto.
Trait for defining a cast operation to some destination type T. Analogous to Into. The inverse of CastFrom. Prefer implementing CastFrom over CastInto because implementing CastFrom automatically provides an implementation of CastInto.
Blanket implementation of a convenience method matches which allows calling can_cast_from with a type parameter. Do not implement this trait.
Trait for defining a cast operation when the source type cannot always be cast to the destination type. Defines a can_cast_from method which borrows the source value, allowing for pattern matching without moving the value. When can_cast_from returns true, calling opt_cast_from must return Some(...), otherwise try_cast_from may panic.
Trait for defining a cast operation when the destination type cannot always be cast from the source type. Defines a can_cast_into method which borrows self, allowing for pattern matching without moving self. If can_cast_into returns true, then calling opt_cast_into must return Some(...), otherwise try_cast_into may panic.